﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Business = VietStar.Library.Business;
using Entity = VietStar.Library.Entity;
using Commom = VietStar.Library.Commom;

public partial class controls_admin_adduser : System.Web.UI.UserControl
{
    Commom.Cryption.MD5Cryption md5 = new Commom.Cryption.MD5Cryption();
    private static DataTable dtThanhVien = new DataTable();
    Entity.ThanhVien entity = new Entity.ThanhVien();
    Business.ThanhVien thanhvien = new Business.ThanhVien();
    Business.QuyenTruyCap quyentruycap = new Business.QuyenTruyCap();
    DataTable dtQuyenTruyCap = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        string action = Request.QueryString["action"].ToString();
        if (Convert.ToInt32(Session["role_login_admin"].ToString()) == 1 || Convert.ToInt32(Session["role_login_admin"].ToString()) == 2 || Convert.ToInt32(Session["role_login_admin"].ToString()) != 2 && action == "edit" || Convert.ToInt32(Session["role_login_admin"].ToString()) != 1 && action == "edit")
        {
            if (!IsPostBack)
            {
                LoadQuyenTruyCap();
                if (Convert.ToInt32(Session["role_login_admin"].ToString()) == 2)
                {
                    ListItem list = ddlQuyenTruyCap.Items.FindByValue("1");
                    ddlQuyenTruyCap.Items.Remove(list);
                }
                //Nếu là thêm mới
                if (action == "add")
                {
                    Page.Title = "Thêm thành viên";
                    lbTitle.Text = "Thêm thành viên";
                }
                //Nếu là sửa
                if (action == "edit")
                {
                    Page.Title = "Sửa Thành viên";
                    lbTitle.Text = "Sửa thành viên";
                    string info = Request.QueryString["info"].ToString();
                    entity.TenDangNhap = info;
                    if (Convert.ToInt32(Session["role_login_admin"].ToString()) == 2 && CheckDeveloper(info) == true)
                    {
                        Response.Redirect("listusers.aspx");
                    }
                    else
                    {
                        dtThanhVien = thanhvien.SelectPrimarykey(entity);
                        txtTenDayDu.Text = dtThanhVien.Rows[0]["TenDayDu"].ToString();
                        datepicker.Text = dtThanhVien.Rows[0]["NgaySinh"].ToString();
                        txtDiaChi.Text = dtThanhVien.Rows[0]["DiaChi"].ToString();
                        txtSoDienThoai.Text = dtThanhVien.Rows[0]["SoDienThoai"].ToString();
                        txtEmail.Text = dtThanhVien.Rows[0]["Email"].ToString();
                        ddlQuyenTruyCap.SelectedValue = dtThanhVien.Rows[0]["QuyenTruyCap"].ToString();
                        cbTrangThai.Checked = Convert.ToBoolean(dtThanhVien.Rows[0]["TrangThai"].ToString());
                        txtTenDangNhap.ReadOnly = true;
                        txtTenDangNhap.Text = dtThanhVien.Rows[0]["TenDangNhap"].ToString();
                        txtMatKhau.TextMode = TextBoxMode.Password;
                        txtMatKhau.Attributes.Add("value", dtThanhVien.Rows[0]["MatKhau"].ToString());
                        txtXacNhanMatKhau.Attributes.Add("value", dtThanhVien.Rows[0]["MatKhau"].ToString());
                    }
                }
            }
        }
        else
        {
            Response.Redirect(Request.UrlReferrer.ToString());
        }
    }
    // Hàm kiểm tra xem có phải là tài khoản của người phát triển web không
    protected bool CheckDeveloper(string tendangnhap)
    {
        try
        {
            bool kt = false;
            entity.TenDangNhap = tendangnhap;
            DataTable dt = thanhvien.SelectPrimarykey(entity);
            if (dt.Rows.Count > 0)
            {
                if (Convert.ToInt32(dt.Rows[0]["QuyenTruyCap"].ToString()) == 1)
                {
                    kt = true;
                }
            }
            return kt;

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void LoadQuyenTruyCap()
    {
        dtQuyenTruyCap = quyentruycap.Select();
        if (dtQuyenTruyCap.Rows.Count > 0)
        {
            ddlQuyenTruyCap.DataSource = dtQuyenTruyCap;
            ddlQuyenTruyCap.DataTextField = "TenQuyen";
            ddlQuyenTruyCap.DataValueField = "Id";
        }
        else
        {
            ddlQuyenTruyCap.DataSource = null;
        }
        ddlQuyenTruyCap.DataBind();        
    }
    protected void btnQuayLai_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/admin/listuser.aspx");
    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        try
        {
            //if (Page.IsValid)
            //{
            //msgCaptcha.Style.Add("display", "none");
            string action = Request.QueryString["action"].ToString();
            //Nếu là sửa
            if (action == "edit")
            {
                Page.Title = "Sửa thành viên";
                if (txtEmail.Text != dtThanhVien.Rows[0]["Email"].ToString())
                {
                    //Kiểm tra tồn tại Email
                    entity.Email = txtEmail.Text;
                    DataTable dtExistEmail = thanhvien.ExistsEmail(entity);
                    if (dtExistEmail.Rows.Count > 0)
                    {
                        msgEmailExists.Style.Add("display", "block");
                        msgError.Style.Add("display", "block");
                        msgSuccess.Style.Add("display", "none");
                        return;
                    }
                    else
                    {
                        msgEmailExists.Style.Add("display", "none");
                    }
                    if (dtExistEmail.Rows.Count == 0)
                    {
                        bool save = false;
                        entity.TenDayDu = txtTenDayDu.Text;
                        entity.NgaySinh = Convert.ToDateTime(datepicker.Text);
                        entity.QuyenTruyCap = Convert.ToInt32(ddlQuyenTruyCap.SelectedValue.ToString());
                        entity.DiaChi = txtDiaChi.Text;
                        entity.SoDienThoai = txtSoDienThoai.Text;
                        entity.Email = txtEmail.Text;
                        entity.TenDangNhap = txtTenDangNhap.Text;
                        if (txtMatKhau.Text != dtThanhVien.Rows[0]["MatKhau"].ToString())
                        {
                            entity.MatKhau = md5.Encrypt(txtMatKhau.Text);
                        }
                        else
                        {
                            entity.MatKhau = dtThanhVien.Rows[0]["MatKhau"].ToString();
                        }
                        entity.TrangThai = cbTrangThai.Checked;
                        save = thanhvien.Update(entity);

                        if (save == true)
                        {
                            msgSuccess.Style.Add("display", "block");
                            msgError.Style.Add("display", "none");
                        }
                        else
                        {
                            msgSuccess.Style.Add("display", "none");
                            msgError.Style.Add("display", "block");
                        }
                    }
                }
                else
                {
                    bool save = false;
                    entity.TenDayDu = txtTenDayDu.Text;
                    entity.NgaySinh = Convert.ToDateTime(datepicker.Text);
                    entity.QuyenTruyCap = Convert.ToInt32(ddlQuyenTruyCap.SelectedValue.ToString());
                    entity.DiaChi = txtDiaChi.Text;
                    entity.SoDienThoai = txtSoDienThoai.Text;
                    entity.Email = txtEmail.Text;
                    entity.TenDangNhap = txtTenDangNhap.Text;
                    if (txtMatKhau.Text != dtThanhVien.Rows[0]["MatKhau"].ToString())
                    {
                        entity.MatKhau = md5.Encrypt(txtMatKhau.Text);
                    }
                    else
                    {
                        entity.MatKhau = dtThanhVien.Rows[0]["MatKhau"].ToString();
                    }
                    entity.TrangThai = cbTrangThai.Checked;
                    save = thanhvien.Update(entity);

                    if (save == true)
                    {
                        msgSuccess.Style.Add("display", "block");
                        msgError.Style.Add("display", "none");
                    }
                    else
                    {
                        msgSuccess.Style.Add("display", "none");
                        msgError.Style.Add("display", "block");
                    }
                }
            }
            //Nếu là thêm mới
            if (action == "add")
            {
                Page.Title = "Thêm thành viên";
                //Kiểm tra tồn tại Email
                entity.Email = txtEmail.Text;
                DataTable dtExistEmail = thanhvien.ExistsEmail(entity);
                if (dtExistEmail.Rows.Count > 0)
                {
                    msgEmailExists.Style.Add("display", "block");
                    msgError.Style.Add("display", "block");
                    msgSuccess.Style.Add("display", "none");
                }
                else
                {
                    msgEmailExists.Style.Add("display", "none");
                }

                //Kiểm tra tồn tại TenDangNhap
                entity.TenDangNhap = txtTenDangNhap.Text;
                DataTable dtExistTenDangNhap = thanhvien.ExistsTenDangNhap(entity);
                if (dtExistTenDangNhap.Rows.Count > 0)
                {
                    msgTenaDangNhapTonTai.Style.Add("display", "block");                    
                    msgError.Style.Add("display", "block");
                    msgSuccess.Style.Add("display", "none");
                }
                else
                {
                    msgTenaDangNhapTonTai.Style.Add("display", "none");
                }
                if (dtExistEmail.Rows.Count == 0 && dtExistTenDangNhap.Rows.Count == 0)
                {
                    bool save = false;
                    entity.TenDayDu = txtTenDayDu.Text;
                    entity.QuyenTruyCap = Convert.ToInt32(ddlQuyenTruyCap.SelectedValue.ToString());
                    entity.NgaySinh = Convert.ToDateTime(datepicker.Text);
                    entity.DiaChi = txtDiaChi.Text;
                    entity.SoDienThoai = txtSoDienThoai.Text;
                    entity.Email = txtEmail.Text;
                    entity.QuyenTruyCap = Convert.ToInt32(ddlQuyenTruyCap.SelectedValue.ToString());
                    entity.TenDangNhap = txtTenDangNhap.Text;
                    entity.MatKhau = md5.Encrypt(txtMatKhau.Text);
                    entity.NgayThamGia = DateTime.Now;
                    entity.TrangThai = cbTrangThai.Checked;
                    save = thanhvien.Insert(entity);

                    if (save == true)
                    {
                        msgSuccess.Style.Add("display", "block");
                        msgError.Style.Add("display", "none");
                    }
                    else
                    {
                        msgSuccess.Style.Add("display", "none");
                        msgError.Style.Add("display", "block");
                    }
                }
            }
            //}
            //else
            // {
            // msgCaptcha.Style.Add("display", "block");
            //}            
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }    
}
